﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Coding4Fun.Phone.Controls;
using MyDeeWeibo.Classes;

namespace MyDeeWeibo.PageViews
{
    public partial class CommentContent : PhoneApplicationPage
    {
        #region CommentShowProperty
        public static readonly DependencyProperty CommentShowProperty =
            DependencyProperty.Register(
            "CommentShow",
            typeof(ObservableCollection<WStatus>),
            typeof(CommentContent),
            new PropertyMetadata((ObservableCollection<WStatus>)null));

        public ObservableCollection<WStatus> CommentShow
        {
            get { return (ObservableCollection<WStatus>)GetValue(CommentShowProperty); }
            set { SetValue(CommentShowProperty, value); }
        }
        #endregion

        public WStatus CommentResult;
        public bool firstLoad;

        public bool replySuccess;
        public bool delSuccess;

        private DeeNetEngine netEngine;
        private DeeCmdBase cmdBase;

        public ObservableCollection<WStatus> DataCollection { get; set; }

        public ObservableCollection<WStatus> BuildCollection()
        {
            DataCollection = new ObservableCollection<WStatus>();
            DataCollection.Add(CommentResult);
            CommentShow = DataCollection;
            return CommentShow;
        }
        
        public CommentContent()
        {
            firstLoad = true;
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (firstLoad)
            {
                BuildCollection();
                WeiboSettings.CommentResult = CommentResult;
                firstLoad = false;
                if (WeiboSettings.CommentResult.user.id == App.CurentUID)
                {
                    ApplicationBarMenuItem delButton = new ApplicationBarMenuItem();
                    delButton.Text = "删除";
                    ApplicationBar.MenuItems.Add(delButton);
                    delButton.Click += new EventHandler(delButton_Click);
                }
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (replySuccess)
            {
                var messagePrompt = new MessagePrompt
                {
                    Title = "提示",
                    Message = "回复成功",
                };
                messagePrompt.Show();
                replySuccess = false;
            }
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            WeiboContent DelCFlag = e.Content as WeiboContent;
            if (DelCFlag != null)
            {
                DelCFlag.delComSuccess = delSuccess;
            }
        }

        private void delButton_Click(object sender, EventArgs e)
        {
            CommentDestroy();
        }

        private void replyButton_Click(object sender, EventArgs e)
        {
            SdkComReply sdkReply = new SdkComReply();
            sdkReply.AccessToken = App.AccessToken;
            sdkReply.cid = WeiboSettings.CommentResult.id;
            sdkReply.id = WeiboSettings.CommentResult.status.id;
            sdkReply.Completed = SendCompleted;
            sdkReply.Show();
        }

        void SendCompleted(object sender, SendCompletedEventArgs e)
        {
            if (e.IsSendSuccess)
                replySuccess = true;
            else
                MessageBox.Show(e.Response, e.ErrorCode.ToString(), MessageBoxButton.OK);
        }

        private void ProfileImage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            App.UserID = WeiboSettings.CommentResult.user.id;
            NavigationService.Navigate(new Uri("/PageViews/UserProfile.xaml", UriKind.Relative));
        }

        private void CommentDestroy()
        {
            // Define a new net engine
            netEngine = new DeeNetEngine();

            // Define a new command base
            cmdBase = new cmdNormalMessages
            {
                acessToken = App.AccessToken,
                //acessTokenSecret = App.AccessTokenSecret,
                Id = WeiboSettings.CommentResult.id
            };


            // Request server, the last parameter is set as default (".json")
            netEngine.RequestCmd(DeeRequestType.COMMENT_DESTROY, cmdBase,
                // Requeset callback
                delegate(DeeRequestType requestType, DeeResponse response)
                {
                    //Deployment.Current.Dispatcher.BeginInvoke(() => ProgressIndicatorIsVisible = false);
                    if (response.errCode == DeeErrCode.SUCCESS)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                        {
                            delSuccess = true;
                            if (NavigationService.CanGoBack)
                            {
                                NavigationService.GoBack();
                            }
                        });
                    }
                    else
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show(response.content, response.errCode.ToString(), MessageBoxButton.OK));
                    }
                });
        }
    }
}